﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqModel;
using Cricket.LinqData;
using System.Web.Services;

public partial class EditSessionResult : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    List<MatchSchedule> matchSchedules;
    Result result;
    CricketGeneral crickGen;

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                bindMatch();
            }

        }
    }

    private void bindMatch()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetMatchesForReportResult> lstResult = new List<GetMatchesForReportResult>();
        lstResult = matchSchedule.GetMatchesForReport();
        if (lstResult.Count > 0)
        {
            drpMatch.DataSource = lstResult;
            drpMatch.DataTextField = "Teams";
            drpMatch.DataValueField = "Id";
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
    }
    protected void drpMatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetTeamsByMatchResult> lstResult = new List<GetTeamsByMatchResult>();
        lstResult = matchSchedule.GetTeamsByMatch(Convert.ToInt32(drpMatch.SelectedValue));
        if (lstResult.Count > 0)
        {
            drpTeam.DataSource = lstResult;
            drpTeam.DataTextField = "TeamName";
            drpTeam.DataValueField = "Id";
            drpTeam.DataBind();
            drpTeam.Items.Insert(0, new ListItem("Select Team", "0"));
        }        
    }
    protected void drpTeam_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindSessionOver();
    }

    private void bindSessionOver()
    {
        MatchSessionResult matchSessionResult = new MatchSessionResult(_dataObject);
        List<MatchSessionResult> lstMatchSessionResult = new List<MatchSessionResult>();
        lstMatchSessionResult = matchSessionResult.GetMatchSessionResultByTeam(Convert.ToInt32(drpMatch.SelectedValue), Convert.ToInt32(drpTeam.SelectedValue));
        if (lstMatchSessionResult.Count > 0)
        {
            rptSession.DataSource = lstMatchSessionResult;
            rptSession.DataBind();
        }
        else
        {
            rptSession.DataSource = null;
            rptSession.DataBind();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Admin"];
        MatchSessionResult matchSessionresult = new MatchSessionResult(_dataObject);         
        matchSessionresult.MatchScheduleId = Convert.ToInt32(drpMatch.SelectedValue);
        matchSessionresult.TeamId = Convert.ToInt32(drpTeam.SelectedValue);

        int Over = 0;

        for (int i = 0; i < rptSession.Items.Count; i++)
        {
            matchSessionresult = new MatchSessionResult(_dataObject);
            TextBox txtId = (TextBox)rptSession.Items[i].FindControl("txtId");
            TextBox txtOver = (TextBox)rptSession.Items[i].FindControl("txtOver");
            TextBox txtRuns = (TextBox)rptSession.Items[i].FindControl("txtRuns");
            if (!string.IsNullOrEmpty(txtOver.Text) && !string.IsNullOrEmpty(txtRuns.Text))
            {
                matchSessionresult.MatchScheduleId = Convert.ToInt32(drpMatch.SelectedValue);
                matchSessionresult.TeamId = Convert.ToInt32(drpTeam.SelectedValue);
                matchSessionresult.Over = Over=Convert.ToInt32(txtOver.Text);
                matchSessionresult.Runs = Convert.ToInt32(txtRuns.Text);
                matchSessionresult.UpdateMatchSessionResult();

                #region Call SP for calculations
                int matchTypeId = 0;
                result = new Result(_dataObject);
                matchSchedule = new MatchSchedule(_dataObject);
                List<GetMatchDetailsResult> lst = new List<GetMatchDetailsResult>();
                lst = matchSchedule.GetMatchDetails(Convert.ToInt32(drpMatch.SelectedValue));
                if (lst.Count > 0)
                {
                    matchTypeId = Convert.ToInt32(lst[0].MatchTypeId);
                }
                result.CalculateWinningResultsForCustomers(Convert.ToInt32(drpMatch.SelectedValue), 1, matchTypeId, Over, Convert.ToInt32(matchSessionresult.TeamId));
                #endregion
            }
        }
    }
}